package com.snoob.springCloudGateway.util;

import com.alibaba.fastjson.JSONObject;
import com.snoob.springcloudalibaba.base.BaseResult;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @描述: 输出json字符串到前端
 * @作者: lixing
 * @日期 2021/6/24 13:28
 */
public class MonoResponseUtil {
    /**
     * @描述 输出json字符串到前端
     * @作者 lixing
     * @日期 2021/6/17 10:08
     * @Param [serverHttpResponse, responseCodeEnum]
     * @return reactor.core.publisher.Mono<java.lang.Void>
     */
    public static Mono<Void> getVoidMono(ServerHttpResponse serverHttpResponse, BaseResult<String> baseResult) {
        serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        DataBuffer dataBuffer = serverHttpResponse.bufferFactory().wrap(JSONObject.toJSONString(baseResult).getBytes());
        return serverHttpResponse.writeWith(Flux.just(dataBuffer));
    }
}
